# -*- coding: UTF-8 -*-
import time

from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pytesseract
from PIL import Image
import requests
from io import BytesIO

username = "你的账号"
password = "你的密码"

if __name__ == '__main__':

    #打开网站
    try:
        # 这里！！！！实现不关闭的重点
        option = webdriver.ChromeOptions()
        option.add_experimental_option("detach", True)
        driver = webdriver.Chrome(options=option)
        driver.get("https://ujnpl.educationgroup.cn/")
    except Exception as e:
        print(e)

    #循环登录直到成功为止
    while True:

        # 判断是否登录成功
        try:
            print("登录开始")
            #如果找到了这个CLASS证明未登录成功
            driver.find_elements_by_class_name("account_login")
            time.sleep(0.5)
            # 点击登录账号按钮
            driver.find_elements_by_class_name("account_login")[0].click()
            time.sleep(0.5)
            # #输入账号
            driver.find_elements_by_id("username_text")[0].clear()
            time.sleep(0.5)
            driver.find_elements_by_id("username_text")[0].send_keys(username)
            # #输入密码
            driver.find_elements_by_id("password_text")[0].clear()
            time.sleep(0.5)
            driver.find_elements_by_id("password_text")[0].send_keys(password)
            # 验证码识别
            response = driver.find_elements_by_class_name("ide_code_image")[0].screenshot_as_png
            BytesIOObj = BytesIO()
            BytesIOObj.write(response)
            image = Image.open(BytesIOObj)
            # image.show()
            image = image.convert("L")  # 转灰度
            threshold = 220  # 阈值可以调整测试
            table = []
            # 转二值化
            for i in range(256):
                if i < threshold:
                    table.append(0)
                else:
                    table.append(1)
            image = image.point(table, '1')
            # 图片显示
            #image.show()
            text = pytesseract.image_to_string(image)
            newStr = ''.join(list(filter(str.isalnum, text)))
            # #点击验证码
            driver.find_elements_by_class_name("ide_code_input")[0].clear()
            time.sleep(0.5)
            driver.find_elements_by_class_name("ide_code_input")[0].send_keys(newStr)
            # #等待验证码框消失
            # ui.WebDriverWait(driver, 60).until_not(EC.presence_of_element_located((By.ID,"layui-layer1")),message="")
            # 点击登录
            time.sleep(1)
            driver.find_elements_by_class_name("login_box_landing_btn")[1].click()
            time.sleep(1)
        except Exception as e:
            print(e)
            #能到这里来证明登录成功了
            break

    time.sleep(1)



    while True:
        try:
            print("图文楼4楼大厅")
            #图书楼四楼大厅
            driver.get("https://ujnpl.educationgroup.cn/xsgl/stdYy/xzzw?sqid=DB5036240E8C29B3E0530B96CE0A4952&id=0ef7e6e83010471b9c1695139b3a4c72")
            #查找空位
            print("查找空位")
            #这个地方很重要，必须延迟一定时间。xzzw标签为后加载内容
            time.sleep(1)
            try:
                print("开始")
                elements_list = driver.find_elements_by_xpath('//*[@id="xzzw"]/div/div/img')
                for obj in elements_list:
                    if obj.get_attribute("src") == "https://ujnpl.educationgroup.cn/xsgl/image/0.png":
                        obj.click()
                        time.sleep(0.5)
                        driver.find_elements_by_xpath('//*[@id="s"]/button')[0].click()
                        print("发现座位！")
                        break
                print("结束")
            except Exception as e:
                print(e)
        except Exception as e:
            print(e)


        try:
            print("读书自修1房")
            #图书楼四楼大厅
            driver.get("https://ujnpl.educationgroup.cn/xsgl/stdYy/xzzw?sqid=DB5036240E8C29B3E0530B96CE0A4952&id=9949552cb5d341728504d71dd2269311")
            #查找空位
            print("查找空位")
            #这个地方很重要，必须延迟一定时间。xzzw标签为后加载内容
            time.sleep(1)
            try:
                print("开始")
                elements_list = driver.find_elements_by_xpath('//*[@id="xzzw"]/div/div/img')
                for obj in elements_list:
                    if obj.get_attribute("src") == "https://ujnpl.educationgroup.cn/xsgl/image/0.png":
                        obj.click()
                        time.sleep(0.5)
                        driver.find_elements_by_xpath('//*[@id="s"]/button')[0].click()
                        print("发现座位！")
                        break
                print("结束")
            except Exception as e:
                print(e)
        except Exception as e:
            print(e)

        try:
            print("读书自修3房")
            # 图书楼四楼大厅
            driver.get(
                "https://ujnpl.educationgroup.cn/xsgl/stdYy/xzzw?sqid=DB5036240E8C29B3E0530B96CE0A4952&id=f5a832adfe7344849f21385edfd3b89d")
            # 查找空位
            print("查找空位")
            # 这个地方很重要，必须延迟一定时间。xzzw标签为后加载内容
            time.sleep(1)
            try:
                print("开始")
                elements_list = driver.find_elements_by_xpath('//*[@id="xzzw"]/div/div/img')
                for obj in elements_list:
                    if obj.get_attribute("src") == "https://ujnpl.educationgroup.cn/xsgl/image/0.png":
                        obj.click()
                        time.sleep(0.5)
                        driver.find_elements_by_xpath('//*[@id="s"]/button')[0].click()
                        print("发现座位！")
                        break
                print("结束")
            except Exception as e:
                print(e)
        except Exception as e:
            print(e)


        try:
            print("读书自修4房")
            # 图书楼四楼大厅
            driver.get(
                "https://ujnpl.educationgroup.cn/xsgl/stdYy/xzzw?sqid=DB5036240E8C29B3E0530B96CE0A4952&id=a861bfc695bf4cb8a387384f76ae270c")
            # 查找空位
            print("查找空位")
            # 这个地方很重要，必须延迟一定时间。xzzw标签为后加载内容
            time.sleep(1)
            try:
                print("开始")
                elements_list = driver.find_elements_by_xpath('//*[@id="xzzw"]/div/div/img')
                for obj in elements_list:
                    if obj.get_attribute("src") == "https://ujnpl.educationgroup.cn/xsgl/image/0.png":
                        obj.click()
                        time.sleep(0.5)
                        driver.find_elements_by_xpath('//*[@id="s"]/button')[0].click()
                        print("发现座位！")
                        break
                print("结束")
            except Exception as e:
                print(e)
        except Exception as e:
            print(e)


        try:
            print("读书自修5房")
            # 图书楼四楼大厅
            driver.get(
                "https://ujnpl.educationgroup.cn/xsgl/stdYy/xzzw?sqid=DB5036240E8C29B3E0530B96CE0A4952&id=c6a9e9b730c64337b40db6da3bfadc69")
            # 查找空位
            print("查找空位")
            # 这个地方很重要，必须延迟一定时间。xzzw标签为后加载内容
            time.sleep(1)
            try:
                print("开始")
                elements_list = driver.find_elements_by_xpath('//*[@id="xzzw"]/div/div/img')
                for obj in elements_list:
                    if obj.get_attribute("src") == "https://ujnpl.educationgroup.cn/xsgl/image/0.png":
                        obj.click()
                        time.sleep(0.5)
                        driver.find_elements_by_xpath('//*[@id="s"]/button')[0].click()
                        print("发现座位！")
                        break
                print("结束")
            except Exception as e:
                print(e)
        except Exception as e:
            print(e)

    #
    # while True:
    #     try:
    #         #点击查询按钮
    #         driver.find_elements_by_id("searchBtn")[0].click()
    #         time.sleep(1)
    #     except Exception as e:
    #         print(e)
    #
    #
    #     for i in range(1, 20):
    #         try:
    #                 try:
    #                     while True:
    #                         driver.find_element_by_class_name('layui-layer layer-anim layui-layer-iframe')
    #                         time.sleep(1)
    #                 except Exception as e:
    #                     #能到这里来就是没有验证码
    #                     #点击向下翻页
    #                     driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/div[2]/div[2]/div/a[2]").click()
    #                     # 等待加载框消失
    #                     ui.WebDriverWait(driver, 5).until_not(EC.visibility_of_element_located((By.CLASS_NAME, "dialog")),message="")
    #
    #                     try:
    #                         xpath_list = driver.find_elements_by_xpath('//*[@id="seats"]/ul/li[@class="free"]')
    #                         for list in xpath_list:
    #                                 print("-------------------\r\n")
    #                                 print(list.get_attribute('textContent'))
    #                                 list.click()
    #                         xpath_list = driver.find_elements_by_xpath('//*[@id="seats"]/ul/li[@class="order"]')
    #                         for list in xpath_list:
    #                             print("-------------------\r\n")
    #                             print(list.get_attribute('textContent'))
    #                             list.click()
    #                     except Exception as e:
    #                         print(e)
    #
    #         except Exception as e:
    #             print(e)


